// https://learn.microsoft.com/en-us/typography/opentype/spec/maxp

// This table establishes the memory requirements for this font.
// Fonts with CFF or CFF2 outlines must use Version 0.5 of this table, specifying only the numGlyphs field.
// Fonts with TrueType outlines must use Version 1.0 of this table, where all data is required.

use super::*;

#[derive(Debug, Clone, Default)]
pub struct Table {
	pub version: Version16Dot16,
	pub num_glyphs: u16,               // The number of glyphs in the font.
	pub max_points: u16,               // Maximum points in a non-composite glyph.
	pub max_contours: u16,             // Maximum contours in a non-composite glyph.
	pub max_composite_points: u16,     // Maximum points in a composite glyph.
	pub max_composite_contours: u16,   // Maximum contours in a composite glyph.
	pub max_zones: u16, // 1 if instructions do not use the twilight zone (Z0), or 2 if instructions do use Z0; should be set to 2 in most cases.
	pub max_twilight_points: u16, // Maximum points used in Z0.
	pub max_storage: u16, // Number of Storage Area locations.
	pub max_function_defs: u16, // Number of FDEFs, equal to the highest function number + 1.
	pub max_instruction_defs: u16, // Number of IDEFs.
	pub max_stack_elements: u16, // Maximum stack depth across Font Program ('fpgm' table), CVT Program ('prep' table) and all glyph instructions (in the 'glyf' table).
	pub max_size_of_instructions: u16, // Maximum byte count for glyph instructions.
	pub max_component_elements: u16, // Maximum number of components referenced at “top level” for any composite glyph.
	pub max_component_depth: u16, // Maximum levels of recursion; 1 for simple components.
}
impl Table {
	pub fn parse(data: &[u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		let version = s.read::<Version16Dot16>()?;
		match version.0 {
			0x00005000 => Some(Self {
				version,
				num_glyphs: s.read::<u16>()?,
				..Default::default()
			}),
			0x00010000 => Some(Self {
				version,
				num_glyphs: s.read()?,
				max_points: s.read()?,
				max_contours: s.read()?,
				max_composite_points: s.read()?,
				max_composite_contours: s.read()?,
				max_zones: s.read()?,
				max_twilight_points: s.read()?,
				max_storage: s.read()?,
				max_function_defs: s.read()?,
				max_instruction_defs: s.read()?,
				max_stack_elements: s.read()?,
				max_size_of_instructions: s.read()?,
				max_component_elements: s.read()?,
				max_component_depth: s.read()?,
			}),
			_ => None,
		}
	}
}
